package com.sokal.easycare.modules.kpoint.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.kpoint.entity.KPointFile;
import com.sokal.easycare.modules.kpoint.entity.KPointMain;
import com.sokal.easycare.modules.kpoint.service.KPointFileService;

/**
 * 体验店附件Controller
 * @author lihaipei
 * @version 2017-03-19
 */
@Controller
@RequestMapping(value = "${adminPath}/kpoint/kPointFile")
public class KPointFileController extends BaseController {

	@Autowired
	private KPointFileService kPointFileService;
	
	@ModelAttribute
	public KPointFile get(@RequestParam(required=false) String id) {
		KPointFile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kPointFileService.get(id);
		}
		if (entity == null){
			entity = new KPointFile();
		}
		return entity;
	}
	
	@RequiresPermissions("kpoint:kPointFile:view")
	@RequestMapping(value = {"list", ""})
	public String list(KPointFile kPointFile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KPointFile> page = kPointFileService.findPage(new Page<KPointFile>(request, response), kPointFile); 
		model.addAttribute("page", page);
		model.addAttribute("kPointFile", kPointFile);
		return "modules/kpoint/kPointFileList";
	}

	@RequiresPermissions("kpoint:kPointFile:view")
	@RequestMapping(value = "form")
	public String form(KPointFile kPointFile, Model model) {
		model.addAttribute("kPointFile", kPointFile);
		return "modules/kpoint/kPointFileForm";
	}

	@RequiresPermissions("kpoint:kPointFile:edit")
	@RequestMapping(value = "save")
	public String save(KPointFile kPointFile, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kPointFile)){
			return form(kPointFile, model);
		}
		kPointFileService.save(kPointFile);
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/kPointFile/?repage";
	}
	
	@RequiresPermissions("kpoint:kPointFile:edit")
	@RequestMapping(value = "delete")
	public String delete(KPointFile kPointFile, RedirectAttributes redirectAttributes) {
		kPointFileService.delete(kPointFile);
		addMessage(redirectAttributes, "删除附件成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/kPointFile/?repage";
	}

	/**
	 * 跳转至文件管理页
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param exprStoreId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toFileList")
	public String toFileList(String exprStoreId, Model model) {
		KPointMain kPointMain = new KPointMain(exprStoreId);
		KPointFile kPointFile = new KPointFile();
		kPointFile.setkPointMain(kPointMain);
		List<KPointFile> kPointFiles = kPointFileService.findList(kPointFile);
		model.addAttribute("kPointFiles", kPointFiles);
		model.addAttribute("exprStoreId", exprStoreId);
		return "modules/kpoint/fileList";
	}

	/**
	 * 保存体验店图片
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param exprStoreFile 
	 * @param exprStoreId 体验店id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveExprStoreFile")
	public String saveExprStoreFile(@RequestParam(value = "exprStoreFile") MultipartFile exprStoreFile, String exprStoreId) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			kPointFileService.saveFiles(exprStoreFile, exprStoreId);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 删除体验店图片
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param exprStoreFileId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deleteExprStoreFile")
	public String deleteExprStoreFile(String exprStoreFileId) {
		Map<String, Object> map = Maps.newHashMap();
		KPointFile kPointFile = new KPointFile(exprStoreFileId);
		kPointFileService.delete(kPointFile);
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 批量修改图片排序号
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param ids
	 * @param sorts
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts) {
		Map<String, Object> map = Maps.newHashMap();
    	for (int i = 0; i < ids.length; i++) {
    		KPointFile kPointFile = new KPointFile(ids[i]);
    		kPointFile.setSort(sorts[i]);
    		kPointFileService.updateSort(kPointFile);
    	}
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
}